package com.example.demos.service.impl;

import com.example.demos.dto.GoodsAddDTO;
import com.example.demos.dto.GoodsQueryParamDTO;
import com.example.demos.entity.Goods;
import com.example.demos.entity.GoodsType;
import com.example.demos.mapper.GoodsMapper;
import com.example.demos.service.GoodsService;
import com.example.demos.util.CheckQueryParams;
import com.example.demos.util.PageBean;
import com.example.demos.vo.GoodsQueryParamVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public PageBean<List<GoodsQueryParamVO>> goodsListByPage(GoodsQueryParamDTO goodsQueryParamDTO) {

        if (goodsQueryParamDTO.getName() != null) {
            goodsQueryParamDTO.setName(goodsQueryParamDTO.getName().trim());
        }
        if (goodsQueryParamDTO.getOrigin() != null) {
            goodsQueryParamDTO.setOrigin(goodsQueryParamDTO.getOrigin().trim());
        }
        if (goodsQueryParamDTO.getProducer() != null) {
            goodsQueryParamDTO.setProducer(goodsQueryParamDTO.getProducer().trim());
        }
        if (goodsQueryParamDTO.getUnit() != null) {
            goodsQueryParamDTO.setUnit(goodsQueryParamDTO.getUnit().trim());
        }

        try {
            if (!CheckQueryParams.checkEmpty(goodsQueryParamDTO, 2)) {
                goodsQueryParamDTO.setInPrice(goodsQueryParamDTO.getInPrice().split("\\.")[0]);
                goodsQueryParamDTO.setOutPrice(goodsQueryParamDTO.getOutPrice().split("\\.")[0]);
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

        try (Page<GoodsQueryParamVO> page = PageHelper.startPage(goodsQueryParamDTO.getPage(), goodsQueryParamDTO.getRows())) {
            goodsMapper.getGoodsList(goodsQueryParamDTO);
            return new PageBean<>(page.getTotal(), page.getResult());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public PageBean<List<GoodsType>> getGoodsType() {
        List<GoodsType> goodsTypeList = goodsMapper.getGoodsTypeList();
        return new PageBean<>(goodsTypeList.size(), goodsTypeList);
    }

    @Override
    public int addGoods(GoodsAddDTO goodsAddDTO) {
        try {
            if (CheckQueryParams.checkEmpty(goodsAddDTO)
                    || goodsAddDTO.getInPrice().doubleValue() < 0
                    || goodsAddDTO.getOutPrice().doubleValue() < 0) {
                return 1;
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        if (goodsAddDTO.getName() != null) {
            goodsAddDTO.setName(goodsAddDTO.getName().trim());
        }
        if (goodsAddDTO.getOrigin() != null) {
            goodsAddDTO.setOrigin(goodsAddDTO.getOrigin().trim());
        }
        if (goodsAddDTO.getProducer() != null) {
            goodsAddDTO.setProducer(goodsAddDTO.getProducer().trim());
        }
        if (goodsAddDTO.getUnit() != null) {
            goodsAddDTO.setUnit(goodsAddDTO.getUnit().trim());
        }
        List<Goods> oldGoods = goodsMapper.selectByNameAndType(goodsAddDTO.getName(), goodsAddDTO.getGoodsType());
        if (oldGoods.size() == 1) {
            return 2;
        }
        Goods goods = new Goods(goodsAddDTO.getName(), goodsAddDTO.getOrigin(), goodsAddDTO.getProducer(),
                goodsAddDTO.getUnit(), goodsAddDTO.getInPrice(), goodsAddDTO.getOutPrice(), goodsAddDTO.getGoodsType());
        int i = goodsMapper.insert(goods);
        return i == 1 ? 3 : 4;
    }

    @Transactional
    @Override
    public boolean updateGoods(BigInteger goodsType, String name, String origin, String producer,
                               String unit, BigDecimal inPrice, BigDecimal outPrice, BigInteger uuid, HttpSession httpSession) {
        Goods goods = new Goods(uuid, name, origin, producer, unit, inPrice, outPrice, String.valueOf(goodsType));
        int i = goodsMapper.updateGoods(goods);
        List<Goods> targetGoods = goodsMapper.selectByNameAndType(name, String.valueOf(goodsType));
        if (targetGoods.size() != 1) {
            throw new RuntimeException("修改的商品类型名称不能与其它商品类型名称重复");
        }
        return i == 1;
    }

    @Override
    public PageBean<Goods> selectOne(Integer id) {
        Goods goods = goodsMapper.selectOne(id);
        return new PageBean<>(1, goods);
    }

    @Override
    public int deleteGoods(Integer id) {
        if (goodsMapper.queryStoreOper(id) == null || goodsMapper.queryStoreOper(id).isEmpty()) {
            return goodsMapper.deleteGoods(id);
        }
        return -1;
    }
}
